package com.pxmeta.app.controller.game;


import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Maps;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.utils.SecurityUtils;
import com.pxmeta.common.utils.StringUtils;
import com.pxmeta.dynamic.service.RedisLockService;
import com.pxmeta.party.entity.Gift;
import com.pxmeta.party.entity.PartyGiveGift;
import com.pxmeta.party.entity.dto.GiveGiftDTO;
import com.pxmeta.party.entity.vo.*;
import com.pxmeta.party.entity.LotteryLuckyUser;
import com.pxmeta.party.service.IGiftService;
import com.pxmeta.party.service.ILotteryLuckyUserService;
import com.pxmeta.party.service.IPartyGiveGiftService;
import com.pxmeta.party.service.IUserGiftWallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.compress.utils.Lists;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

@RestController
@RequestMapping("/party/gift")
@RequiredArgsConstructor
@Api(value = "语聊房礼物",tags = "语聊房礼物")
public class GiftController {

    private final IGiftService giftService;
    private final ILotteryLuckyUserService lotteryLuckyUserService;
    private final IUserGiftWallService userGiftWallService;

    private final RedisLockService redisLockService;
    private final IPartyGiveGiftService partyGiveGiftService;


    @GetMapping("/getTurntableGiftList")
    @ApiOperation("获取转盘礼物列表")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "类型：1欢乐转盘，2黄金转盘"),
            }
    )
    public AjaxResult<List<Gift>> getTurntableGiftList(@RequestParam Integer type){
        List<Gift> list = giftService.getTurntableGiftList(type);
        return AjaxResult.success(list);
    }


    @SaCheckLogin
    @ApiOperation("判断是否可以开启转盘")
    @GetMapping("/checkIsOpenLottery")
    public AjaxResult checkIsOpenLottery(){
        Long userId = SecurityUtils.getUserId();
        Boolean isOpen = giftService.checkIsOpenLottery(userId);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("isOpen",isOpen);
        return AjaxResult.success(map);
    }

    @SaCheckLogin
    @ApiOperation("判断是否可以开启星际探险")
    @GetMapping("/checkIsOpenAdAstra")
    public AjaxResult checkIsOpenAdAstra(){
        Long userId = SecurityUtils.getUserId();
        Boolean isOpen = giftService.checkIsOpenAdAstra(userId);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("isOpen",isOpen);
        return AjaxResult.success(map);
    }

    @SaCheckLogin
    @ApiOperation("判断是否可以开启吃货大排档")
    @GetMapping("/checkIsOpenFoodStall")
    public AjaxResult checkIsOpenFoodStall(){
        Long userId = SecurityUtils.getUserId();
        Boolean isOpen = giftService.checkIsOpenFoodStall(userId);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("isOpen",isOpen);
        return AjaxResult.success(map);
    }

    @SaCheckLogin
    @ApiOperation("判断是否可以开启钻洞")
    @GetMapping("/checkIsOpenWormhole")
    public AjaxResult checkIsOpenWormhole(){
        Long userId = SecurityUtils.getUserId();
        Boolean isOpen = giftService.checkIsOpenWormhole(userId);
        HashMap<String, Object> map = Maps.newHashMap();
        map.put("isOpen",isOpen);
        return AjaxResult.success(map);
    }


    @GetMapping("/turntableLottery")
    @ApiOperation("转盘抽奖")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "类型：1欢乐转盘，2黄金转盘"),
                    @ApiImplicitParam(name = "count", value = "数量，默认为1"),
            }
    )
    public AjaxResult<List<TurntableLotteryVO>> turntableLottery(@RequestParam Integer type,
                                                                 @RequestParam(defaultValue = "1") Integer count){
        Long userId = SecurityUtils.getUserId();
        List<TurntableLotteryVO> list = giftService.turntableLottery(userId,count,type);
        return AjaxResult.success(list);
    }

    @GetMapping("/getUserGiftList")
    @ApiOperation("获取用户礼物列表")
    public AjaxResult<List<Gift>> getUserGiftList(){

        Long userId = SecurityUtils.getUserId();
        List<Gift> list = giftService.getUserGiftList(userId);

        return AjaxResult.success(list);
    }


    @GetMapping("/getLotteryRanking")
    @ApiOperation("获取抽奖排行榜")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "type", value = "类型：1日榜，2总榜"),
                    @ApiImplicitParam(name = "page", value = "页码"),
                    @ApiImplicitParam(name = "size", value = "每页记录数"),
            }
    )
    public AjaxResult<List<LuckyUserRankingVO>> getLotteryRanking(@RequestParam Integer type,
                                                          @RequestParam Integer page,
                                                          @RequestParam Integer size){
        List<LuckyUserRankingVO> list = lotteryLuckyUserService.getLotteryRanking(type, page, size);
        return AjaxResult.success(list);
    }

    @GetMapping("/getCurrentUserRanking")
    @ApiOperation("获取当前用户排名")
    public AjaxResult getCurrentUserRanking(@RequestParam Integer type){
        Long userId = SecurityUtils.getUserId();
        LuckyUserRankingVO vo = lotteryLuckyUserService.getCurrentUserRanking(userId,type);

        return AjaxResult.success(vo);
    }


    @GetMapping("/getLuckyUsers")
    @ApiOperation("获取幸运用户列表")
    public AjaxResult<List<LotteryLuckyUser>> getLuckyUsers(@RequestParam(defaultValue = "1") Integer type){

        LambdaQueryWrapper<LotteryLuckyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(LotteryLuckyUser::getPrice, 100);
        wrapper.eq(LotteryLuckyUser::getType, type);
        wrapper.orderByDesc(LotteryLuckyUser::getCreateTime)
                .last("limit 10");
        List<LotteryLuckyUser> list = lotteryLuckyUserService.list(wrapper);
        return AjaxResult.success(list);
    }

    @GetMapping("/getStartList")
    @ApiOperation("获取虫洞初始化数据")
    public AjaxResult getStartList(){
        List<Integer> list = Lists.newArrayList();
        list.add(1);
        list.add(5);
        list.add(66);
        Map<String, Object> map = Maps.newHashMap();
        map.put("list",list);
        return AjaxResult.success(map);
    }

    @GetMapping("/getWormholeStartInfo")
    @ApiOperation(value = "获取虫洞启动前信息")
    public AjaxResult getWormholeStartInfo(@RequestParam Integer count){
        Map<String, Object> productInfo = Maps.newHashMap();
        productInfo.put("avatarFrame","https://pengxinapp.oss-cn-beijing.aliyuncs.com/avatar/%E9%B2%B8%E8%90%BD.png");
        productInfo.put("price",10 * count);
        productInfo.put("name","老玩家专属");
        return AjaxResult.success(productInfo);
    }



    @ApiOperation("开始虫洞")
    @GetMapping("/startWormhole")
    public AjaxResult startWormhole(@RequestParam Integer count){
        Long userId = SecurityUtils.getUserId();
        giftService.startWormhole(userId,count);
        return AjaxResult.success();
    }


    @ApiOperation("进入下一关虫洞")
    @GetMapping("/nextWormhole")
    public AjaxResult<WormholeLotteryVO> nextWormhole(@RequestParam Integer count){

        Long userId = SecurityUtils.getUserId();
        WormholeLotteryVO vo = giftService.nextWormhole(userId, count);
        return AjaxResult.success(vo);
    }


    @ApiOperation("结束虫洞游戏")
    @GetMapping("/endWormhole")
    public AjaxResult endWormhole(@RequestParam Long wormholeId){
        List<Gift> list = giftService.endWormhole(wormholeId);
        return AjaxResult.success(list);
    }


    @GetMapping("/getLotteryRecord")
    @ApiOperation("获取抽奖记录")
    public AjaxResult<List<Map<String, Object>>> getLotteryRecord(){
        Long userId = SecurityUtils.getUserId();
        List<Map<String, Object>> list = lotteryLuckyUserService.getLotteryRecord(userId);
        return AjaxResult.success(list);
    }

    @GetMapping("/getGiftList")
    @ApiOperation("获取礼物列表")
    public AjaxResult<List<Gift>> getGiftList(@RequestParam Integer page,
                                  @RequestParam Integer size,
                                              @RequestParam(required = false) Integer type){
        List<Gift> list = giftService.getGiftList(page,size,type).getRecords();
        return AjaxResult.success(list);
    }

    @GetMapping("/getUserGiftWall")
    @ApiOperation("获取用户礼物墙")
    public AjaxResult<UserGiftWallListVO> getUserGiftWall(@RequestParam Long userId){
        UserGiftWallListVO vo = userGiftWallService.getUserGiftWallList(userId);
        return AjaxResult.success(vo);
    }

    @GetMapping("/getUserGiftWallIntro")
    @ApiOperation("获取用户礼物墙简介")
    public AjaxResult<UserGiftWallListVO> getUserGiftWallIntro(@RequestParam Long userId){
        UserGiftWallListVO vo = userGiftWallService.getUserGiftWallIntro(userId);
        return AjaxResult.success(vo);
    }


    @PostMapping("/giveGift")
    @ApiOperation("赠送礼物")
    public AjaxResult giveGift(@RequestBody GiveGiftDTO dto){

        Long userId = SecurityUtils.getUserId();
        if (CollectionUtil.isNotEmpty(dto.getTargetUsernames())){
            giftService.give(userId, dto.getTargetUsernames(), dto.getGiftId(), dto.getPartyId(),dto.getType(),dto.getCount());
        }else {
            giftService.give(userId, dto.getTargetUserId(), dto.getGiftId(),dto.getPartyId(), dto.getType(),dto.getCount());
        }

        return AjaxResult.success();
    }


    @GetMapping("/getGiftItems")
    @ApiOperation("获取用户礼物明细")
    public AjaxResult<List<GiftItemsVO>> getGiftItems(@RequestParam String partyId,
                                   @RequestParam String username){
        List<GiftItemsVO> vo = partyGiveGiftService.getGiveItems(partyId, username);
        return AjaxResult.success(vo);
    }

    @GetMapping("/getPartyCharmValues")
    @ApiOperation("获取房间魅力值")
    public AjaxResult getPartyCharmValues(@RequestParam String partyId, @RequestParam(required = false) List<String> usernames){
        List<PartyCharmValuesVO> list = partyGiveGiftService.getPartyCharmValues(partyId,usernames);
        return AjaxResult.success(list);
    }


    @DeleteMapping("/cleanPartyCharmValues")
    @ApiOperation("清空房间魅力值")
    public AjaxResult cleanPartyCharmValues(@RequestParam String partyId){
        LambdaQueryWrapper<PartyGiveGift> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PartyGiveGift::getPartyId, partyId);
        partyGiveGiftService.remove(wrapper);
        return AjaxResult.success();
    }





    @PostMapping("/lottery")
    @SaCheckLogin
    @ApiOperation(value = "抽奖")
    public AjaxResult lottery(@RequestBody GiveGiftDTO dto){
        Integer giftId = dto.getGiftId();
        Long userId = SecurityUtils.getUserId();
        List<LotteryGiftVO> list = giftService.lottery(userId, giftId,dto.getTargetUsernames(),dto.getPartyId());
        return AjaxResult.success(list);

    }





}
